﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xeml.Document;
using System.Diagnostics;
using XemlUtilities;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using XemlUtilities.DataSet;
using Xeml.Sampling.Contracts;
using System.Data.SqlClient;
using Xeml.SQLDataProviders;

namespace XemlTest
{
    [TestClass]
    public class UtXemlDataSet
    {
        [TestCategory("Nightly"), TestCategory("Weekly"), TestCategory("ShoppingCart"), TestMethod()] //, Ignore()]
        public void UpdateDatasetInXemlRepository()
        {
            IDataProvider dataProvider = new GmdDataProvider();
            foreach (Guid g in General.GetXemlIds((SqlConnection)dataProvider.BuildConnection()))
            {
                XemlDataSet.Save(
                    General.GetDocument(g)
                    , dataProvider
                    , (string s, Guid d) => { return true; });
            }
        }

        [TestCategory("Nightly"), TestCategory("Weekly"), TestCategory("ShoppingCart"), TestMethod()] //, Ignore()]
        public void testReplicaGroupDescription()
        {
            IDataProvider dataProvider = new GmdDataProvider();
            foreach (Guid g in General.GetXemlIds((SqlConnection)dataProvider.BuildConnection()))
            {
                //using (XemlDataSet xds = new XemlDataSet(GetDocument(Guid.Parse("061DBDEB-F3DD-4AFA-ACFD-54E83479C3DD"))))
                using (XemlDataSet xds = new XemlDataSet(General.GetDocument(g), dataProvider))
                {
                    Debug.WriteLine(xds.Document.Single().Name);
                    Debug.Write(string.Join(string.Empty, xds.ReplicaGroup.OrderBy(x => x.Id).Select(x => string.Concat(x.Label, Environment.NewLine))));
                }
            }
        }

        [TestCategory("Nightly"), TestCategory("Weekly"), TestCategory("ShoppingCart"), TestMethod(), Ignore()]
        public void ReplicaGrouplabelss()
        {
            IDataProvider dataProvider = new GmdDataProvider();
            using (XemlDataSet xds = new XemlDataSet(General.GetDocument(Guid.Parse("061DBDEB-F3DD-4AFA-ACFD-54E83479C3DD")), dataProvider))
            {
                foreach (var item in xds.ReplicaGroup.OrderBy(x => x.Id))
                {
                    Debug.WriteLine(item.Label);
                }
            }
        }

        [TestCategory("Nightly"), TestCategory("Weekly"), TestCategory("ShoppingCart"), TestMethod()] //, Ignore()]
        public void testReplicaGroups()
        {
            Dictionary<Guid, int> res = new Dictionary<Guid, int>(4);
            res.Add(Guid.Parse("10b38aaf-b977-4950-85b8-f4775f66658d"), 5);
            res.Add(Guid.Parse("ba487064-8b46-4408-812c-b4e5083ad7c2"), 7);
            res.Add(Guid.Parse("57545514-4007-4234-9ba0-f37fb2794081"), 16);
            res.Add(Guid.Parse("061DBDEB-F3DD-4AFA-ACFD-54E83479C3DD"), 36);

            Dictionary<Guid, XemlDocument> documents = new Dictionary<Guid, XemlDocument>(4);
            documents.Add(Guid.Parse("10b38aaf-b977-4950-85b8-f4775f66658d"), General.GetDocument(Guid.Parse("10b38aaf-b977-4950-85b8-f4775f66658d")));
            documents.Add(Guid.Parse("ba487064-8b46-4408-812c-b4e5083ad7c2"), General.GetDocument(Guid.Parse("ba487064-8b46-4408-812c-b4e5083ad7c2")));
            documents.Add(Guid.Parse("57545514-4007-4234-9ba0-f37fb2794081"), General.GetDocument(Guid.Parse("57545514-4007-4234-9ba0-f37fb2794081")));
            documents.Add(Guid.Parse("061DBDEB-F3DD-4AFA-ACFD-54E83479C3DD"), General.GetDocument(Guid.Parse("061DBDEB-F3DD-4AFA-ACFD-54E83479C3DD")));

            int count = 0;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            List<TimeSpan> tata = new List<TimeSpan>();

            Parallel.For(1, 1000000, new ParallelOptions() { MaxDegreeOfParallelism = 12 }, i =>
            {
                TimeSpan start = sw.Elapsed;
                IDataProvider dataProvider = new GmdDataProvider();
                foreach (Guid g in General.GetXemlIds((SqlConnection)dataProvider.BuildConnection()))
                {
                    using (XemlDataSet xds = new XemlDataSet(documents[g], dataProvider))
                    {
                        Debug.Assert(xds.ReplicaGroup.Count == res[g]);
                    }
                }
                TimeSpan duration = sw.Elapsed - start;
                lock (tata)
                {
                    tata.Add(duration);
                }
                Debug.WriteLine(Interlocked.Increment(ref count));
            });

            Debug.WriteLine("avg: {0}", tata.Average(x => x.Milliseconds));
        }

    }
}
